//
// SoapTrace.Net - A soap message monitoring utility. 
//
// Copyright (C) 2006 Matthew Ward
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// Matthew Ward (mrward@users.sourceforge.net)

using System;
using System.Collections;
using System.Text;

namespace SoapTrace.Core.Formatters
{
	/// <summary>
	///   A collection that stores <see cref='Line'/> objects.
	/// </summary>
	public class LineCollection : CollectionBase
	{
		
		/// <summary>
		///   Initializes a new instance of <see cref='LineCollection'/>.
		/// </summary>
		public LineCollection( )
		{
		}
		
		/// <summary>
		/// Reads the text and returns a <see cref="LineCollection"/>.
		/// </summary>
		public static LineCollection GetLines( string text )
		{
			if( text == null )
			{
				throw new ArgumentNullException( "text" );
			}
			
			LineCollection Lines = new LineCollection( );
			StringBuilder CurrentLine = new StringBuilder( );
			
			for( int i = 0; i < text.Length; ++i )
			{
				char currentChar = text[ i ];
				switch( currentChar )
				{
					case '\n':
						CurrentLine = AddLine( Lines, CurrentLine, "\n" );
						break;
					case '\r':
						// Check if we have a newline as the next character.
						if( IsNewLineChar( i + 1, text ) )
						{
							CurrentLine = AddLine( Lines, CurrentLine, "\r\n" );
							++i;
						}
						else
						{
							CurrentLine = AddLine( Lines, CurrentLine, "\r" );
						}
						break;
					default:
						CurrentLine.Append( currentChar );
						break;
				}
			}
			
			// Add last line.
			Lines.Add( new Line( CurrentLine.ToString( ) ) );

			return Lines;
		}
		
		/// <summary>
		///   Initializes a new instance of <see cref='LineCollection'/> based on another <see cref='LineCollection'/>.
		/// </summary>
		/// <param name='val'>
		///   A <see cref='LineCollection'/> from which the contents are copied
		/// </param>
		public LineCollection( LineCollection val )
		{
			this.AddRange( val );
		}
		
		/// <summary>
		///   Initializes a new instance of <see cref='LineCollection'/> containing any array of <see cref='Line'/> objects.
		/// </summary>
		/// <param name='val'>
		///       A array of <see cref='Line'/> objects with which to intialize the collection
		/// </param>
		public LineCollection( Line[ ] val )
		{
			this.AddRange( val );
		}
		
		/// <summary>
		///   Represents the entry at the specified index of the <see cref='Line'/>.
		/// </summary>
		/// <param name='index'>The zero-based index of the entry to locate in the collection.</param>
		/// <value>The entry at the specified index of the collection.</value>
		/// <exception cref='ArgumentOutOfRangeException'><paramref name='index'/> is outside the valid range of indexes for the collection.</exception>
		public Line this[ int index ]
		{
			get
			{
				return ( ( Line )( List[ index] ) );
			}
			set
			{
				List[ index ] = value;
			}
		}
		
		/// <summary>
		///   Adds a <see cref='Line'/> with the specified value to the 
		///   <see cref='LineCollection'/>.
		/// </summary>
		/// <param name='val'>The <see cref='Line'/> to add.</param>
		/// <returns>The index at which the new element was inserted.</returns>
		/// <seealso cref='LineCollection.AddRange'/>
		public int Add( Line val )
		{
			return List.Add( val );
		}
		
		/// <summary>
		///   Copies the elements of an array to the end of the <see cref='LineCollection'/>.
		/// </summary>
		/// <param name='val'>
		///    An array of type <see cref='Line'/> containing the objects to add to the collection.
		/// </param>
		/// <seealso cref='LineCollection.Add'/>
		public void AddRange( Line[ ] val )
		{
			for( int i = 0; i < val.Length; i++ )
			{
				this.Add( val [ i ] );
			}
		}
		
		/// <summary>
		///   Adds the contents of another <see cref='LineCollection'/> to the end of the collection.
		/// </summary>
		/// <param name='val'>
		///    A <see cref='LineCollection'/> containing the objects to add to the collection.
		/// </param>
		/// <seealso cref='LineCollection.Add'/>
		public void AddRange( LineCollection val )
		{
			for( int i = 0; i < val.Count; i++ )
			{
				this.Add( val[ i ] );
			}
		}
		
		/// <summary>
		///   Gets a value indicating whether the 
		///    <see cref='LineCollection'/> contains the specified <see cref='Line'/>.
		/// </summary>
		/// <param name='val'>The <see cref='Line'/> to locate.</param>
		/// <returns>
		/// <see langword='true'/> if the <see cref='Line'/> is contained in the collection; 
		///   otherwise, <see langword='false'/>.
		/// </returns>
		/// <seealso cref='LineCollection.IndexOf'/>
		public bool Contains( Line val )
		{
			return List.Contains( val );
		}
		
		/// <summary>
		///   Copies the <see cref='LineCollection'/> values to a one-dimensional <see cref='Array'/> instance at the 
		///    specified index.
		/// </summary>
		/// <param name='array'>The one-dimensional <see cref='Array'/> that is the destination of the values copied from <see cref='LineCollection'/>.</param>
		/// <param name='index'>The index in <paramref name='array'/> where copying begins.</param>
		/// <exception cref='ArgumentException'>
		///   <para><paramref name='array'/> is multidimensional.</para>
		///   <para>-or-</para>
		///   <para>The number of elements in the <see cref='LineCollection'/> is greater than
		///         the available space between <paramref name='arrayIndex'/> and the end of
		///         <paramref name='array'/>.</para>
		/// </exception>
		/// <exception cref='ArgumentNullException'><paramref name='array'/> is <see langword='null'/>. </exception>
		/// <exception cref='ArgumentOutOfRangeException'><paramref name='arrayIndex'/> is less than <paramref name='array'/>'s lowbound. </exception>
		/// <seealso cref='Array'/>
		public void CopyTo( Line[ ] array, int index )
		{
			List.CopyTo( array, index );
		}
		
		/// <summary>
		///    Returns the index of a <see cref='Line'/> in 
		///       the <see cref='LineCollection'/>.
		/// </summary>
		/// <param name='val'>The <see cref='Line'/> to locate.</param>
		/// <returns>
		///   The index of the <see cref='Line'/> of <paramref name='val'/> in the 
		///   <see cref='LineCollection'/>, if found; otherwise, -1.
		/// </returns>
		/// <seealso cref='LineCollection.Contains'/>
		public int IndexOf( Line val )
		{
			return List.IndexOf( val );
		}
		
		/// <summary>
		///   Inserts a <see cref='Line'/> into the <see cref='LineCollection'/> at the specified index.
		/// </summary>
		/// <param name='index'>The zero-based index where <paramref name='val'/> should be inserted.</param>
		/// <param name='val'>The <see cref='Line'/> to insert.</param>
		/// <seealso cref='LineCollection.Add'/>
		public void Insert( int index, Line val )
		{
			List.Insert( index, val );
		}
		
		/// <summary>
		///  Returns an enumerator that can iterate through the <see cref='LineCollection'/>.
		/// </summary>
		/// <seealso cref='IEnumerator'/>
		public new LineEnumerator GetEnumerator( )
		{
			return new LineEnumerator( this );
		}
		
		/// <summary>
		///   Removes a specific <see cref='Line'/> from the <see cref='LineCollection'/>.
		/// </summary>
		/// <param name='val'>The <see cref='Line'/> to remove from the <see cref='LineCollection'/>.</param>
		/// <exception cref='ArgumentException'><paramref name='val'/> is not found in the Collection.</exception>
		public void Remove( Line val )
		{
			List.Remove( val );
		}
		
		/// <summary>
		///   Enumerator that can iterate through a LineCollection.
		/// </summary>
		/// <seealso cref='IEnumerator'/>
		/// <seealso cref='LineCollection'/>
		/// <seealso cref='Line'/>
		public class LineEnumerator : IEnumerator
		{
			IEnumerator baseEnumerator;
			IEnumerable temp;
			
			/// <summary>
			///   Initializes a new instance of <see cref='LineEnumerator'/>.
			/// </summary>
			public LineEnumerator( LineCollection mappings )
			{
				this.temp = ( ( IEnumerable )( mappings ) );
				this.baseEnumerator = temp.GetEnumerator( );
			}
			
			/// <summary>
			///   Gets the current <see cref='Line'/> in the <seealso cref='LineCollection'/>.
			/// </summary>
			public Line Current
			{
				get
				{
					return ( ( Line )( baseEnumerator.Current ) );
				}
			}
			
			object IEnumerator.Current
			{
				get
				{
					return baseEnumerator.Current;
				}
			}
			
			/// <summary>
			///   Advances the enumerator to the next <see cref='Line'/> of the <see cref='LineCollection'/>.
			/// </summary>
			public bool MoveNext( )
			{
				return baseEnumerator.MoveNext( );
			}
			
			/// <summary>
			///   Sets the enumerator to its initial position, which is before the first element in the <see cref='LineCollection'/>.
			/// </summary>
			public void Reset( )
			{
				baseEnumerator.Reset( );
			}
		}
		
		/// <summary>
		/// Checks that the index refers to the last line in the set.
		/// </summary>
		/// <param name="index">The line index.</param>
		/// <param name="lines">The total number of lines.</param>
		/// <returns><see langword="true"/> if it is the last line.</returns>
		static bool IsLastLine( int index, int lines )
		{
			return index == lines - 1;
		}
		
		/// <summary>
		/// Adds the line to the collection and creates a new string builder.
		/// </summary>
		/// <param name="lines">The lines to add the line to.</param>
		/// <param name="text">The line's text.</param>
		/// <param name="terminator">The line's terminator string.</param>
		/// <returns>A new <see cref="StringBuilder"/>.</returns>
		static StringBuilder AddLine( LineCollection lines, StringBuilder text, string terminator )
		{
			lines.Add( new Line( text.ToString( ), terminator ) );
			return new StringBuilder( );
		}
		
		/// <summary>
		/// Checks that the next character at the specified index is a 
		/// new line character.
		/// </summary>
		/// <param name="index">The character index to check.</param>
		/// <param name="text">The text that will be checked.</param>
		/// <returns><see langword="true"/> if the character is a new line;
		/// <see langword="false"/> if the character is not a new line or the
		/// index is passed the end of the text.</returns>
		static bool IsNewLineChar( int index, string text )
		{
			bool Match = false;
			
			if( index < text.Length )
			{
				if( text[ index ] == '\n' )
				{
					Match = true;
				}
			}
			
			return Match;
		}
	}
}
